package com.chb.service.impl;

import com.chb.dao.DeptDao;
import com.chb.model.entity.Dept;
import com.chb.model.entity.Staff;
import com.chb.model.vo.PageBean;
import com.chb.model.vo.Result;
import com.chb.service.DeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：张家琪
 * @date ：Created in 2023/10/9 17:54
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private HttpSession session;

    /**
     * @param
     * @return
     * @create by: 张77
     * @description:查询所有部门
     * @create time: 2023/10/9 18:11
     */
    @Override
    public Result findAllDept(Integer current, Integer pageSize) {
        Staff staff = (Staff) session.getAttribute("staff");
        //设置分页的大小
        PageHelper.startPage(current, pageSize);
        //查询所有的部门信息
        List<Dept> dept = deptDao.findAllDept();
        //查询该门店下部门人员信息
        List<Dept> deptCount = deptDao.findAllDeptCount(staff.getBranchid());
        for (int i = 0; i < dept.size(); i++) {
            for (Dept dept2 : deptCount) {
                if (dept.get(i).getDeptid().equals(dept2.getDeptid())) {
                    dept.get(i).setDeptCount(dept2.getDeptCount());
                    break;
                } else {
                    dept.get(i).setDeptCount(0);
                }
            }
        }
        PageInfo<Dept> pageInfo = new PageInfo<>(dept);
        return new Result(200, "查询成功", pageInfo);
    }

    /**
     * @param
     * @return
     * @create by: 张77
     * @description:添加部门
     * @create time: 2023/10/9 19:37
     */
    @Override
    public Result add(Dept dept) {
        Integer integer = deptDao.add(dept);
        return new Result(200, "添加成功", integer);
    }

    /**
     * @param
     * @return
     * @create by: 张77
     * @description:删除部门
     * @create time: 2023/10/9 20:18
     */
    @Override
    public Result del(Integer deptid) {
        System.out.println("deptid" + deptid + "*************");
        Integer integer = deptDao.del(deptid);
        return new Result(200, "删除成功", integer);
    }

    /**
     * @param
     * @return
     * @create by: 张77
     * @description:修改部门信息
     * @create time: 2023/10/10 9:39
     */
    @Override
    public Result updept(Dept dept) {
        Integer integer = deptDao.updept(dept);
        return new Result(200, "修改成功", integer);
    }


    /**
     * @param page     当前页数
     * @param pageSize 每页展示的页数
     * @param deptid   部门id
     * @return com.chb.model.vo.Result
     * @create by: 张77
     * @description: 获取部门下的员工信息
     * @create time: 2023/10/10 11:19
     */
    @Override
    public Result selectStaffByDeptId(Integer page, Integer pageSize, Integer deptid) {
        // 获取session
        Staff staff = (Staff) session.getAttribute("staff");
        //进行分页
        //1.设置分页大小
        PageHelper.startPage(page, pageSize);
        //2.进行查询
        List<Staff> staffList = deptDao.selectStaffByDeptId(deptid, staff.getBranchid());
        //3.封装分页结果
        PageInfo<Staff> pageInfo = new PageInfo<>(staffList);
        //4.再次封装
        PageBean<Staff> pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pageBean);
    }
}